/*****************************************************************************
*    Copyright (C)2003 Ali Corporation. All Rights Reserved.
*
*    File:    nim_sp5769.c
*
*    Description:    This file contains sp5769 basic function in LLD.
*    History:
*           Date            Athor        Version          Reason
*	    ============	=============	=========	=================
*	1.  Aug.25.2003		Justin Wu		 Ver 0.1	Create file.
*****************************************************************************/

#include <sys_config.h>
#include <retcode.h>
#include <types.h>

#include <api/libc/printf.h>
#include <bus/i2c/i2c.h>

#include "nim_sp5769.h"

struct QPSK_TUNER_CONFIG_EXT *stSP5769_dev_id[MAX_TUNER_SUPPORT_NUM] = {NULL};
static UINT32 stSP5769_tuner_cnt = 0;
/*****************************************************************************
* INT32 nim_sl1935_init(UINT32 para, struct QPSK_TUNER_CONFIG_EXT * ptrTuner_Config)
*
* Tuner SL1935 Initialization
*
* Arguments:
*  Parameter1: UINT32 para		: Input Parameter
*  Parameter2: struct QPSK_TUNER_CONFIG_EXT * ptrTuner_Config		: pointer for Tuner configuration structure
*
* Return Value: INT32			: Result
*****************************************************************************/
INT32 nim_sp5769_init(UINT32 * tuner_id, struct QPSK_TUNER_CONFIG_EXT * ptrTuner_Config)
{
	struct QPSK_TUNER_CONFIG_EXT *stSP5769_ptr = NULL;
	/* check Tuner Configuration structure is available or not */
	if (ptrTuner_Config == NULL||stSP5769_tuner_cnt>=MAX_TUNER_SUPPORT_NUM)
		return ERR_FAILUE;
	stSP5769_ptr = (struct QPSK_TUNER_CONFIG_EXT*)MALLOC(sizeof(struct QPSK_TUNER_CONFIG_EXT));
	if(!stSP5769_ptr)
		return ERR_FAILUE;
	stSP5769_dev_id[stSP5769_tuner_cnt] = stSP5769_ptr;
	MEMCPY(stSP5769_ptr, ptrTuner_Config, sizeof(struct QPSK_TUNER_CONFIG_EXT));
	*tuner_id = stSP5769_tuner_cnt;
	stSP5769_tuner_cnt++;
	return SUCCESS;
}
/*****************************************************************************
* INT32 nim_sp5769_control(UINT32 freq, UINT8 bs, UINT8 cp)
*
* Tuner write operation
*
* Arguments:
*  Parameter1: UINT32 freq		: Synthesiser programmable divider
*  Parameter2: UINT8 bs			: Baseband path select
*  Parameter3: UINT8 cp			: Charge pump current
*
* Return Value: INT32			: Result
*****************************************************************************/
INT32 nim_sp5769_control(UINT32 tuner_id, UINT32 freq, UINT8 bs)
{
	UINT8 data[4];
	UINT8 pos;
	UINT16 divider;
	struct QPSK_TUNER_CONFIG_EXT *stSP5769_ptr = NULL;
	/* check Tuner Configuration structure is available or not */
	if (tuner_id>=stSP5769_tuner_cnt||tuner_id>=MAX_TUNER_SUPPORT_NUM)
		return ERR_FAILUE;
	stSP5769_ptr = stSP5769_dev_id[tuner_id];
	pos = (freq > 1450 ? 1 : 0);
	divider = freq*1000/500;//(freq * 16000 + 1000) / 2000;					/* Debug? */

	data[0] = (UINT8)((divider >> 8) & 0x7f);
	data[1] = (UINT8)divider;
	data[2] = 0x82;
	data[3] = (UINT8)((pos ? 0x40 : 0x44));
	return i2c_write(stSP5769_ptr->i2c_type_id, stSP5769_ptr->cTuner_Base_Addr, data, 4);
}

/*****************************************************************************
* INT32 nim_sp5769_status(UINT8 *lock)
*
* Tuner read operation
*
* Arguments:
*  Parameter1: UINT8 *lock		: Phase lock status
*
* Return Value: INT32			: Result
*****************************************************************************/
INT32 nim_sp5769_status(UINT32 tuner_id, UINT8 *lock)
{
	INT32 result;
	UINT8 data;
	struct QPSK_TUNER_CONFIG_EXT *stSP5769_ptr = NULL;
	/* check Tuner Configuration structure is available or not */
	if (tuner_id>=stSP5769_tuner_cnt||tuner_id>=MAX_TUNER_SUPPORT_NUM)
	{
		*lock = 0;
		return ERR_FAILUE;
	}
	stSP5769_ptr = stSP5769_dev_id[tuner_id];
	result = i2c_read(stSP5769_ptr->i2c_type_id, stSP5769_ptr->cTuner_Base_Addr, &data, 1);
	*lock = ((data & 0x40) >> 6);

	return result;
}
